package com.stars.services.impl;

import com.stars.common.base.BaseCode;
import com.stars.common.base.BaseMsg;
import com.stars.common.base.ResultMsg;
import com.stars.services.BaseConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luodajie on 2019/1/5.
 */
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
public class BaseConfigServiceImpl<M extends Mapper<T>, T> implements BaseConfigService<T> {
    @Autowired
    protected M mapper;

    public void setMapper(M mapper) {
        this.mapper = mapper;
    }

    @Override
    public ResultMsg insert(T entity) throws Exception{
        int i = mapper.insert(entity);
        if (i == 0) {
            return ResultMsg.customMsg(BaseCode.SYSTEM_ERROR, BaseMsg.SAVE_FAILURE, null);
        }
        return ResultMsg.success();
    }

    @Override
    public ResultMsg insertSelective(T entity) throws Exception{
        int i = mapper.insertSelective(entity);
        if (i == 0) {
            return ResultMsg.customMsg(BaseCode.SYSTEM_ERROR, BaseMsg.SAVE_FAILURE, null);
        }
        return ResultMsg.success();
    }

    @Override
    public ResultMsg delete(T entity) throws Exception{
        int i = mapper.delete(entity);
        if (i == 0) {
            return ResultMsg.customMsg(BaseCode.SYSTEM_ERROR, BaseMsg.DEL_FAILURE, null);
        }
        return ResultMsg.success();
    }

    @Override
    public ResultMsg deleteById(Object ids) throws Exception{
        if(null == ids){
            return ResultMsg.failure("没有要删除的数据！");
        }
        //禁止删除id为root的节点
        if("root".equals(ids)){
            throw new Exception("不能删除根节点！");
        }
        T t = mapper.selectByPrimaryKey(ids);
        int i = mapper.deleteByPrimaryKey(ids);
        if (i == 0) {
            return ResultMsg.customMsg(BaseCode.SYSTEM_ERROR, BaseMsg.DEL_FAILURE, null);
        }
        return ResultMsg.success(t);
    }

    @Override
    public ResultMsg deleteByIds(String  [] ids) throws Exception {
        if(null == ids){
            return ResultMsg.failure(BaseMsg.DEL_FAILURE);
        }
        List<Object> resultMsgList = new ArrayList<>();
        for(String id:ids){
            //禁止删除id为root的节点
            if("root".equals(id)){
                throw new Exception("不能删除根节点！");
            }
          ResultMsg resultMsg =  deleteById(id);
            resultMsgList.add(resultMsg.getData());
        }
        return ResultMsg.success(resultMsgList);
    }

    @Override
    public ResultMsg updateById(T entity) throws Exception{
        int i = mapper.updateByPrimaryKey(entity);
        if (i == 0) {
            return ResultMsg.customMsg(BaseCode.SYSTEM_ERROR, BaseMsg.UPDATE_FAILURE, null);
        }
        return ResultMsg.success();
    }

    @Override
    public ResultMsg updateSelectiveById(T entity) throws Exception{
        int i = mapper.updateByPrimaryKeySelective(entity);
        if (i == 0) {
            return ResultMsg.customMsg(BaseCode.SYSTEM_ERROR, BaseMsg.UPDATE_FAILURE, null);
        }
        return ResultMsg.success();
    }

}
